package com.gzr.controller;

import com.gzr.pojo.Article;
import com.gzr.pojo.PageBean;
import com.gzr.pojo.Result;
import com.gzr.server.ArticleServer;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleServer articleServer;

    /**
     * 新增文章
     * @param article
     * @return
     */
    @PostMapping
    public Result article(@RequestBody @Validated Article article) {
        articleServer.add(article);
        return Result.success();
    }

    /**
     * 文章列表(条件分页)
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param categoryId 文章分类ID
     * @param state
     * @return 发布状态
     */
    @GetMapping
    public Result<PageBean<Article>> list(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> result = articleServer.list(pageNum,pageSize,categoryId,state);
        return Result.success(result);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article article = articleServer.findById(id);
        return Result.success(article);
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated Article article) {
        articleServer.update(article);
        return Result.success();
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id) {
        articleServer.delete(id);
        return Result.success();
    }
}
